package com.lyy.king.common.rpc.processors;

import com.lyy.king.common.rpc.annotations.LearningApiInterface;
import com.lyy.king.common.rpc.config.DobboConfig;
import com.lyy.king.common.rpc.config.LearningInterfaceScan;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.env.Environment;

import java.util.Map;

/**
 * dubbo注册
 */
public class DubboServiceBeanPostProcessor implements BeanPostProcessor {

    private static RegistryConfig registryConfig = null;
    private static ApplicationConfig applicationConfig = null;

    @Autowired
    private Environment environment;
    @Autowired
    DobboConfig dobboConfig;

    public DubboServiceBeanPostProcessor() {

    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // 初始化配置
        initConfig();
        // 获取当前注册 bean的 Class对象
        Class<?> beanClass = bean.getClass();
        for (Map.Entry<Class<?>, Class<?>> entry : LearningInterfaceScan.interfaceToImplementation.entrySet()) {
            if (entry.getValue() == beanClass) {
                // 找到了一个需要注册到Dubbo的bean
                registerDubboService(bean, entry);
            }
        }
        return bean;
    }

    /**
     * 注册 dubbo 的 bean
     *
     * @param bean
     * @param entry
     */
    private void registerDubboService(Object bean, Map.Entry<Class<?>, Class<?>> entry) {
        // 获取接口的注解
        Class<?> interfaceClass = entry.getKey();
        LearningApiInterface annotation = interfaceClass.getAnnotation(LearningApiInterface.class);
        String version = annotation.version();
        String group = annotation.group();


        ServiceConfig<Object> service = new ServiceConfig<>();
        service.setApplication(applicationConfig);
        service.setRegistry(registryConfig);
        service.setInterface(entry.getKey());
        service.setGroup(getGroupName(dobboConfig.getAppName(), group));
        service.setVersion(version);
        service.setRef(bean);
        service.export();
    }

    /**
     * 初始化配置
     */
    private void initConfig() {
        if (registryConfig == null) {
            initRegistryConfig();
        }

        if (applicationConfig == null) {
            initApplicationConfig();
        }
    }


    /**
     * 初始化注册配置
     */
    private synchronized void initRegistryConfig() {
        if (registryConfig != null) {
            return;
        }
        registryConfig = new RegistryConfig(dobboConfig.getRegistryAddress());
        registryConfig.setProtocol(dobboConfig.getProtocolName());
        registryConfig.setPort(dobboConfig.getProtocolPort());
        registryConfig.setParameters(dobboConfig.getParameters());
    }


    /**
     * 初始化应用配置
     */
    private synchronized void initApplicationConfig() {
        if (applicationConfig != null) {
            return;
        }
        applicationConfig = new ApplicationConfig(dobboConfig.getAppName());
    }

    /**
     * 拼接 group名称
     *
     * @param appName
     * @param groupName
     * @return
     */
    private String getGroupName(String appName, String groupName) {
        return StringUtils.isBlank(groupName) ? appName : groupName;
    }
}